package com.sen.patterns.structural.composite.ex1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 组合体部件，验证了组合体类的部件也可以是组合体
 * 
 * @author sen
 * 
 */
public abstract class CompositeEquipment extends Equipment {

	private int i = 0;
	// 定义一个集合来存放子部件
	private List<Equipment> equipmentList = new ArrayList<Equipment>();

	public CompositeEquipment(String name) {
		super(name);
	}

	public boolean add(Equipment equipment) {
		this.equipmentList.add(equipment);
		return true;
	}

	public double discountPrice() {
		double discountPrice = 0.0;
		Iterator<Equipment> iter = equipmentList.iterator();
		for (; iter.hasNext();) {
			discountPrice += iter.next().discountPrice();
		}
		return discountPrice;
	}

	public double netPrice() {
		double netPrice = 0.0;
		Iterator<Equipment> iter = equipmentList.iterator();
		for (; iter.hasNext();) {
			netPrice += iter.next().netPrice();
		}
		return netPrice;
	}

	public Iterator iter() {
		return equipmentList.iterator();
	}

	// 重写Iterator的next方法
	public Object next() {
		if (hasNext()) {
			return equipmentList.get(i++);
		}
		return false;
	}

	// 重写Iterator的hasNext方法
	public boolean hasNext() {
		return i < equipmentList.size();
	}
}
